package com.springmvc.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 查看请求/响应时间的过滤器
 * Created by Administrator on 2016/12/20.
 */
public class ReqResFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest)servletRequest;

        SimpleDateFormat sdf = new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " );

        String start = sdf.format(new Date());
        filterChain.doFilter(servletRequest, servletResponse);
        String end = sdf.format(new Date());

        System.out.println("请求地址：[" + req.getRequestURI() + "] ===>> 请求时间：" + start + "，结束时间：" + end);
    }

    @Override
    public void destroy() {

    }
}
